package chattingRoomUpdate;

import chattingRoom.ReleaseUtils;

import javax.swing.plaf.synth.SynthOptionPaneUI;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.Socket;

/**
 * 使用多线程封装了
 */
public class ClientReceive implements Runnable {

    private Socket client;
    private boolean isRunning;
    private DataInputStream dataInputStream;
    private boolean isFile;
    FileInputStream fileInputStream;

    public ClientReceive(Socket client) {
        this.client = client;
        try {
            dataInputStream = new DataInputStream(client.getInputStream());
            fileInputStream = new FileInputStream("C:\\Users\\鱼尾\\Desktop\\微信图片_20220320212742.jpg");
            this.isRunning = true;

        } catch (IOException e) {
            release();
            System.out.println("接收功能的这一块出现了问题");
        }
    }


    public String receiveMessage() {
        String message = "";
        try {
            message = dataInputStream.readUTF();
        } catch (IOException e) {
            release();
            System.out.println("接收消息出现了问题");
        }
        return message;
    }

    public void release() {
        this.isRunning = false;
        ReleaseUtils.close(dataInputStream, client);
        ReleaseUtils.close(fileInputStream);
    }


    @Override
    public void run() {
        while (isRunning) {
            String message = receiveMessage();

            if (message.startsWith("该用户")) {
                System.out.println(message);
                this.isFile = true;
                if (isFile) {
                    byte[] bytes = new byte[1024];
                    int len = 0;
                    //写入自己将要上传的信息内容
                    while (true) {
                        try {
                            if (!((len = fileInputStream.read(bytes)) != -1)) break;
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                        try {
                            client.getOutputStream().write(bytes, 0, len);
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }

                    //告诉服务器已经传输结束
                    try {
                        client.shutdownOutput();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }

                    //接收服务端传过来的信息
                    while (true) {
                        try {
                            if (!((len = client.getInputStream().read(bytes)) != -1)) break;
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                        System.out.println(new String(bytes, 0, len));
                    }

                    //释放资源
                    try {
                        fileInputStream.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            } else {
                if (!message.equals("")) {
                    //如果接收到的信息不为空，那么就打印处理

                    System.out.println(message);
                }
            }

        }
    }

}
